#!/usr/bin/env python
import os
import sys
from fixlib.args import args_dispatcher


def main():
    argv = sys.argv[1:]

    if len(argv) < 2:
        print("Usage: argsdispatcher <cmd> <infile> [<arg>...]")
        sys.exit(1)

    cmd = argv.pop(0)
    infile = argv.pop(0)

    if not os.path.isfile(infile):
        print("File not found {infile}")
        sys.exit(1)

    cmd_args = args_dispatcher([cmd], use_which=True, argv=argv)
    args = []
    if cmd in cmd_args:
        args = cmd_args[cmd]

    with open(infile, mode="r") as f:
        file_content = f.read()

    file_content = file_content.replace("@FIX_ARGSDISPATCHER@", " ".join(args))

    print(file_content)


if __name__ == "__main__":
    main()
